@extends('admin.index.layout')
@section('title', $title)
@section('content')
    
        <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                评论管理
                <small>评论列表</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
                <li><a href="#">评论管理</a></li>
                <li class="active">评论列表</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">


                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">查看评论列表</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                            <form action="{{ url('/admin/comment/index') }}">
                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="input-group margin">
                                            <input type="text" class="form-control" value="{{ $request['keywords'] or '' }}" name="keywords">
                                            <span class="input-group-btn">
                                              <button class="btn btn-info btn-flat" type="button">搜索</button>
                                            </span>
                                        </div>
                                    </div>

                                    <div class="col-md-2  col-md-offset-6">
                                        <div class="form-group">
                                        
                                            <label>Select</label>
                                            <select name="num" class="form-control">
                                                <option>10</option>
                                                <option>25</option>
                                                <option>50</option>
                                                <option>100</option>
                                            </select>

                                        </div>
                                    </div>
                                </div>
                            </form>
                                
                            <table id="example1" class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>商品名</th>
                                    <th>评论用户</th>
                                    <th>评论内容</th>
                                    <th>评论时间</th>
                                    <th>回复内容</th>
                                    <th>回复时间</th>
                                    <th>评价星级</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody class="tbody">

                                @foreach($data as $k => $u)
                                <tr>
                                    <td class="ids">{{ $u -> id}}</td>
                                    <td class="gname">{{$u -> gname }}</td>
                                    <td class="uname">{{$u -> uname }}</td>
                                    <td class="content">{{$u -> content }}</td>
                                    <td class="time">{{$u -> time }}</td>
                                    <td class="reply">{{$u -> reply }}</td>
                                    <td class="rtime">{{$u -> rtime }}</td>
                                    <td class="level">{{$u -> level }}</td>
                                    <td><i class="del fa fa-fw fa-remove"></i></td>
                                </tr>
                                @endforeach
                                </tbody>
                            </table>
                          {!! $data ->appends($request) -> render() !!}  
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->

    <script type="text/javascript">
        window.onload = function()
        {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $('.reply').attr('title',"双击回复");
            // 删除评论
            $('.fa-remove').on('click', function()
            {
                //获取当前对象
                var t = $(this);
                var id = $(this).parent().parent().find('.ids').html();
                
                $.ajax({
                    type:"POST",
                    url:"{{ asset('admin/comment/del') }}",
                    data:{id:id},
                    success:function(data)
                    {
                        a = $(t).parent().parent().parent().html();
                        // alert(a)
                        $(t).parent().parent().empty();
                        // alert(data);
                       console.log(data);
                    },
                    error:function()
                    {
                       // console.log(data);
                        alert('异常');
                    }
                });
            });

            // 管理回复
            $('.reply').on('dblclick', dblClick);
            function dblClick(){
                // 获取当前id
                var id = $(this).parent().find('.ids').html();
                var t = $(this);
                // 准备input标签
                var inp = $("<input type='text' />");
                // 获取原有值
                var oldReply = $(this).html();
                // 添加原来内容到input
                inp.val(oldReply);

                // 修改td的值
                $(this).html(inp);
                inp.select();
                $(inp).on('blur',function(){
                    // 获取新的回复内容
                    var newReply = inp.val();

                    $.ajax({
                        type:"POST",
                        url:"{{ asset('admin/comment/reply') }}",
                        data:{id:id,reply:newReply},
                        success:function(data)
                        {
                            if(data == 1){
                                t.html(newReply);
                            }else{
                                alert('回复失败');
                                t.html(oldReply);
                            }
                        },
                        error:function()
                        {
                            alert('异常,请重试！！！');
                        }
                    });
                    // 注册事件
                    t.on('dblclick',dblClick);
                });
                t.unbind('dblclick');
            };

          
        }
    </script>
    


@endsection